package com.css.zfzx.sjcj.modules.fourthgenerations.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.common.utils.BeanUtil;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY18Entity;
import com.css.zfzx.sjcj.modules.fourthgenerations.repository.QhGeoFourthgenerationsNativeRepository;
import com.css.zfzx.sjcj.modules.fourthgenerations.repository.QhGeoFourthgenerationsRepository;
import com.css.zfzx.sjcj.modules.fourthgenerations.repository.entity.QhGeoFourthgenerationsEntity;
import com.css.zfzx.sjcj.modules.fourthgenerations.service.QhGeoFourthgenerationsService;
import com.css.zfzx.sjcj.modules.fourthgenerations.viewobjects.QhGeoFourthgenerationsQueryParams;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author tao
 * @date 2020-11-27
 */
@Service
public class QhGeoFourthgenerationsServiceImpl implements QhGeoFourthgenerationsService {

	@Autowired
    private QhGeoFourthgenerationsRepository qhGeoFourthgenerationsRepository;
    @Autowired
    private QhGeoFourthgenerationsNativeRepository qhGeoFourthgenerationsNativeRepository;

    @Override
    public JSONObject queryQhGeoFourthgenerationss(QhGeoFourthgenerationsQueryParams queryParams, int curPage, int pageSize) {
        Page<QhGeoFourthgenerationsEntity> qhGeoFourthgenerationsPage = this.qhGeoFourthgenerationsNativeRepository.queryQhGeoFourthgenerationss(queryParams, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(qhGeoFourthgenerationsPage);
        return jsonObject;
    }

    @Override
    public JSONObject queryQhGeoFourthgenerationss(QhGeoFourthgenerationsQueryParams queryParams) {
        List<QhGeoFourthgenerationsEntity> qhGeoFourthgenerationsPage = this.qhGeoFourthgenerationsNativeRepository.listQhGeoFourthgenerationss(queryParams);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", qhGeoFourthgenerationsPage);
        return jsonObject;
    }

    @Override
    public QhGeoFourthgenerationsEntity getQhGeoFourthgenerations(String id) {
        QhGeoFourthgenerationsEntity qhGeoFourthgenerations = this.qhGeoFourthgenerationsRepository.findById(id).orElse(null);
         return qhGeoFourthgenerations;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhGeoFourthgenerationsEntity saveQhGeoFourthgenerations(QhGeoFourthgenerationsEntity qhGeoFourthgenerations) {
        //String uuid = UUIDGenerator.getUUID();
        //qhGeoFourthgenerations.setOgrFid(uuid);
        qhGeoFourthgenerations.setCreateUser(PlatformSessionUtils.getUserId());
        qhGeoFourthgenerations.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.qhGeoFourthgenerationsRepository.save(qhGeoFourthgenerations);
        return qhGeoFourthgenerations;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhGeoFourthgenerationsEntity updateQhGeoFourthgenerations(QhGeoFourthgenerationsEntity qhGeoFourthgenerations) {
        qhGeoFourthgenerations.setUpdateUser(PlatformSessionUtils.getUserId());
        qhGeoFourthgenerations.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        String id = qhGeoFourthgenerations.getOgrFid();
        QhGeoFourthgenerationsEntity one = qhGeoFourthgenerationsRepository.findById(id).get();
        BeanUtil.copyPropertiesIgnoreNull(qhGeoFourthgenerations, one);
        this.qhGeoFourthgenerationsRepository.save(one);
        return qhGeoFourthgenerations;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteQhGeoFourthgenerationss(List<String> ids) {
        List<QhGeoFourthgenerationsEntity> qhGeoFourthgenerationsList = this.qhGeoFourthgenerationsRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(qhGeoFourthgenerationsList) && qhGeoFourthgenerationsList.size() > 0) {
            for(QhGeoFourthgenerationsEntity qhGeoFourthgenerations : qhGeoFourthgenerationsList) {
                this.qhGeoFourthgenerationsRepository.delete(qhGeoFourthgenerations);
            }
        }
    }


}
